package com.ssyt.examinationsystem.controller;

import com.ssyt.examinationsystem.entity.RoleInfo;
import com.ssyt.examinationsystem.entity.StudentInfo;
import com.ssyt.examinationsystem.entity.TeacherInfo;
import com.ssyt.examinationsystem.service.IRoleInfoService;
import com.ssyt.examinationsystem.service.IStudentInfoService;
import com.ssyt.examinationsystem.service.ITeacherInfoService;
import com.ssyt.examinationsystem.service.impl.RoleInfoServiceImpl;
import com.ssyt.examinationsystem.service.impl.StudentInfoServiceImpl;
import com.ssyt.examinationsystem.service.impl.TeacherInfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

/**
 * Servlet implementation class UserInfoServlet
 */
public class LoginInfoController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginInfoController() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 创建一个学生业务层对象
        IStudentInfoService stuService = new StudentInfoServiceImpl();
        // 创建一个角色业务层对象
        IRoleInfoService roleService = new RoleInfoServiceImpl();
        // 创建一个老师业务层对象
        ITeacherInfoService teaService = new TeacherInfoServiceImpl();

        // 得到输出流对象
        PrintWriter out = response.getWriter();
        // 得到地址栏上标识
        String flag = request.getParameter("flag");
        if ("initLogin".equals(flag)) {
            initLogin(roleService, request, response);
        } else if ("login".equals(flag)) {
            login(stuService, teaService, request, response, out);
        }

        out.flush();
        out.close();

    }

    /**
     * 登录用户的方法
     *
     * @param userService
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void login(IStudentInfoService stuService, ITeacherInfoService teaService, HttpServletRequest request,
                       HttpServletResponse response, PrintWriter out) throws ServletException, IOException {
        // 获取登录页面的数据
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        Integer roleId = Integer.valueOf(request.getParameter("roleId"));
        // 创建一个角色对象
        RoleInfo role = new RoleInfo();
        role.setRoleId(roleId);
        // 创建一个学生对象
        StudentInfo stu = new StudentInfo();
        stu.setAccount(account);
        stu.setPassword(password);
        stu.setRole(role);
        // 创建一个教师和管理员对象
        TeacherInfo teacher = new TeacherInfo();
        teacher.setAccount(account);
        teacher.setPassword(password);
        teacher.setRole(role);

        if (roleId == 3) {
            // 调用学生的业务方法
            if (stuService.login(stu)) {
                Map<String, Object> student = stuService.getStuByAcc(account, password);
                String stuId = String.valueOf(student.get("stuId"));
                request.getSession().setAttribute("stuId", stuId);
                request.getSession().setAttribute("account", account);
                request.getSession().setAttribute("password", password);
                request.getSession().setAttribute("roleId", roleId);
                // 转发到WEB-INF/index.jsp
                request.getRequestDispatcher("WEB-INF/views/menu/menu.jsp").forward(request, response);
            } else {
                out.print("<script>alert('登录失败');location.href='WEB-INF/login.jsp'</script>");
            }
        } else {
            // 调用教师和管理员的业务方法
            if (teaService.login(teacher)) {

                request.getSession().setAttribute("roleId", roleId);
                // 转发到WEB-INF/index.jsp
                request.getRequestDispatcher("WEB-INF/views/menu/menu.jsp").forward(request, response);
            } else {
                out.print("<script>alert('登录失败');location.href='WEB-INF/login.jsp'</script>");
            }
        }

    }

    /**
     * 初始化登录的方法
     *
     * @param userService
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void initLogin(IRoleInfoService roleService, HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 调用角色的查询所有业务方法
        List<RoleInfo> roleList = roleService.getRoleInfos();
        // 将集合保存在请求对象中
        request.setAttribute("roleList", roleList);
        // 转发到WEB-INF/login.jsp页面
        request.getRequestDispatcher("WEB-INF/login.jsp").forward(request, response);
    }

}
